package com.entities.projectteam.detail;


import java.util.Iterator;
import java.util.List;

import com.core.protocols.ICustomDatasourceDelegate;
import com.core.protocols.IDescriptionViewDelegate;
import com.core.protocols.IDetailTableViewDelegate;
import com.core.protocols.IServiceLayerDelegate;
import com.core.protocols.ITabSheetDatasource;
import com.core.servicelayer.HibernateServiceLayer;
import com.core.ui.controller.base.HibernateDetailController;
import com.core.ui.customcell.TabSheetItemCell;
import com.core.utils.KeyValueDictionary;
import com.core.utils.LocalizableString;
import com.entities.projectteam.servicelayer.TeamMemberListLayer;
import com.entities.user.servicelayer.UserListLayer;
import com.model.KeyValue;
import com.model.project.TeamMember;
import com.model.user.User;


public class TeamMemberDetailController
        extends HibernateDetailController
        implements IDetailTableViewDelegate, IDescriptionViewDelegate, IServiceLayerDelegate, ITabSheetDatasource {


	private String               memberId;
	private TeamMemberListLayer listLayer       = null;
	private TeamMember           memberItem;

	private final int            userRequestUserInfo = 301;
	private final int            roleRequestUserInfo = 302;
	private UserListLayer        userServiceLayer;
	private KeyValueDictionary   userDictionary;



	public TeamMemberDetailController() {

		listLayer = new TeamMemberListLayer();

	}



	public String getMemberId () {

		return memberId;
	}



	public void setMemberId ( String memberId ) {

		this.memberId = memberId;
	}



	/**
	 * ICustomDatasource
	 * 
	 * 
	 */


	@Override
	public void reloadData () {

		this.userPerformFetchItems();

		Integer result = null;
		try {
			result = Integer.parseInt(this.memberId);
		} catch ( NumberFormatException e ) {
			result = null;
		}


		memberItem = (TeamMember) listLayer.objectByUNID(result, null, this);

	}



	@Override
	public void setCustomDatasourceDelegate ( ICustomDatasourceDelegate delegate ) {

		// TODO Auto-generated method stub

	}



	/**
     *
     *
     *
     *
     */


	@Override
	public int tabSheetCellsCount () {

		// TODO Auto-generated method stub
		return 0;
	}



	@Override
	public TabSheetItemCell tabSheetCellAtIndex ( int index ) {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public String tabsPanelStyleClassName () {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public String descriptionData () {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public int detailTableViewItemsCount ( String userInfo ) {

		int result = 0;
		String userInfoUC = userInfo.toUpperCase();
		if ( userInfoUC.equals("BASE") ) {
			result = 2;
		}
		return result;
	}



	@Override
	public String detailTableViewItemAtIndex ( String userInfo,
	                                           int index ) {

		String result = null;

		if ( userInfo.toUpperCase().equals("BASE") ) {
			result = this.userBaseItemAtIndex(index);
		}
		return result;
	}



	@Override
	public <T> void responseResponseItemsList ( final List <T> items,
	                                            final Object request,
	                                            final Exception exception ) {

		if ( request == null ) {
			return;
		}

		HibernateServiceLayer serviceLayer = (HibernateServiceLayer) request;
		Integer userInfo = (Integer) serviceLayer.getUserInfo();
		if ( userInfo == null ) {
			return;
		}


		switch ( userInfo ) {
			case userRequestUserInfo: {
				this.userResponseItems(items);
				break;
			}
			case roleRequestUserInfo: {

				break;
			}
			default: {
				break;
			}
		}
	}



	private String userBaseItemAtIndex ( int index ) {

		LocalizableString localizableString = LocalizableString.sharedInstance();

		String result = null;

		String style = null;
		String propertyValue = null;
		String data = null;

		boolean readOnly = this.isReadOnly();

		switch ( index ) {
			case 0: {
				data = this.userCell(memberItem, readOnly);
				break;
			}
			case 1: {
				style = "-single";
				if ( memberItem != null ) {
					propertyValue = memberItem.getDescription();
				}
				String caption = localizableString.localizable("entity.Team.field.description");
				data = this.textAreaControl("description", caption, propertyValue, readOnly);
				break;
			}
			default: {
				break;
			}
		}
		result = String.format("<li class=\"property-item%s\"><div class=\"wrap\">%s</li>", style, data);
		return result;
	}



	private String userCell ( TeamMember member,
	                          boolean readOnly ) {

		LocalizableString localizableString = LocalizableString.sharedInstance();

		String result = null;
		KeyValue propertyValue = null;
		if ( member != null ) {
			User user = member.getTeamMemberCategory().getUser();
			if ( user != null ) {
				String id = String.format("%d", user.getId());
				String name = user.getName();
				propertyValue = new KeyValue(id, name);
			}
		}
		String caption = localizableString.localizable("entity.Team.field.user");
		result = this.selectControl("user", caption, propertyValue, userDictionary, readOnly);
		return result;

	}



	private void userPerformFetchItems () {

		userServiceLayer = new UserListLayer();
		userServiceLayer.setUserInfo(new Integer(userRequestUserInfo));
		userServiceLayer.setLayerDelegate(this);
		userServiceLayer.performRequestWithFilter(null, null);

	}



	private <T> void userResponseItems ( List <T> items ) {

		userDictionary = new KeyValueDictionary();
		userDictionary.put(null, "-");
		Iterator <T> iterator = items.iterator();
		while ( iterator.hasNext() ) {
			User user = (User) iterator.next();
			userDictionary.put(String.format("%d", user.getId()), user.getName());
		}

	}


}
